import { createRouter, createWebHistory } from "vue-router";
import TreeView from "../views/TreeView.vue";
import TreeHome from "../views/TreeHome.vue";

const mockReplace = (branch) => {
  //   debugger;
  return new Promise((resolve) => {
    setTimeout(() => {
      if (branch === "feat/nie/latest") {
        resolve(true);
      } else {
        resolve(false);
      }
    }, 200);
  });
};

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: "/tree/:branch+",
      name: "tree-main",
      component: TreeHome,
    },
    {
      path: "/tree/:branch+/:dir*",
      name: "tree-dir",
      component: TreeView,
      async beforeEnter(to, from) {
        try {
          debugger;
          console.log('解析逻辑')
          const res = await mockReplace(to.params.branch.join("/"));
          //   补全审核逻辑
          if (res) {
            return { name: "tree-main", params: { branch: to.params.branch } };
          }
          return true;
        } catch (err) {
          // 还是继续跳比较合理
          console.error("路由跳转失败:", err);
          return false; // 中断当前的导航
        }
      },
    },
  ],
});

export default router;
